#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan Database Package

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_38"
__date__    = "2014/02/09"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import traceback
import datetime
try:
	import pytz
	is_pytz = True
	_utc = pytz.timezone('UTC')
except:
	is_pytz = False
	_utc= None

# Mikan
from esipc import ESiPCProperty
from mikan.core import AbstractMikan

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class MikanNullObject(object):
	pass

class AbstractMikanDatabase(AbstractMikan):

	def get_request(self):
		return self.FRequest

	def __init__(self, request):
		# super
		super(AbstractMikanDatabase, self).__init__()

		# variable
		self.FRequest   = request

	def connect(self):
		pass

	def close(self):
		pass

	def commit(self):
		pass

	def rollback(self):
		pass

	def get_version(self):
		return ""

	def get_name(self):
		return self.__class__.__str__()

	# ----------------------------------------------------
	## Property
	request = ESiPCProperty(get_request, None, None)
	version = ESiPCProperty(get_version, None, None)
	name = ESiPCProperty(get_name, None, None)

class AbstractMikanItemDatabase(AbstractMikanDatabase):

	def get_idt_import(self):
		try:
			if self.FIDTImport != None:
				return self.FIDTImport
		except:
			pass
		from mikan.idt.importidt import MikanImportIDT
		self.FIDTImport = MikanImportIDT(self)
		return self.FIDTImport

	def get_idt_export(self):
		try:
			if self.FIDTExport != None:
				return self.FIDTExport
		except:
			pass
		from mikan.idt.exportidt import MikanExportIDT
		self.FIDTExport = MikanExportIDT(self)
		return self.FIDTExport

	def get_idt_ac(self):
		try:
			if self.FIDTAC != None:
				return self.FIDTAC
		except:
			pass
		from mikan.idt.acidt import MikanACIDT
		self.FIDTAC = MikanACIDT(self)
		return self.FIDTAC

	def get_idt_cio(self):
		try:
			if self.FIDTCIO != None:
				return self.FIDTCIO
		except:
			pass
		from mikan.idt.cioidt import MikanCIOIDT
		self.FIDTCIO = MikanCIOIDT(self)
		return self.FIDTCIO

	def get_idt_validate(self):
		try:
			if self.FIDTValidate != None:
				return self.FIDTValidate
		except:
			pass
		from mikan.idt.validateidt import MikanValidateIDT
		self.FIDTValidate = MikanValidateIDT(self)
		return self.FIDTValidate

	def get_idt_sequence(self):
		try:
			if self.FIDTSequence != None:
				return self.FIDTSequence
		except:
			pass
		from mikan.idt.sequenceidt import MikanSequenceIDT
		self.FIDTSequence = MikanSequenceIDT(self)
		return self.FIDTSequence

	def get_idt_relation(self):
		try:
			if self.FIDTRelation != None:
				return self.FIDTRelation
		except:
			pass
		from mikan.idt.relationidt import MikanRelationIDT
		self.FIDTRelation = MikanRelationIDT(self)
		return self.FIDTRelation

	def get_idt_form(self):
		try:
			if self.FIDTForm != None:
				return self.FIDTForm
		except:
			pass
		from mikan.idt.form import MikanFormIDT
		self.FIDTForm = MikanFormIDT(self)
		return self.FIDTForm

	def __init__(self, request):
		# super
		super(AbstractMikanItemDatabase, self).__init__(request)

	def creates(self, mlist):
		if self.islogdebug:
			self.logger.debug(mlist)
		for e in mlist:
			self.create(e)

	def removes(self, mlist):
		if self.islogdebug:
			self.logger.debug(mlist)
		for e in mlist:
			self.remove(e)

	def updates(self, mlist):
		if self.islogdebug:
			self.logger.debug(mlist)
		for e in mlist:
			self.update(e)

	def copy(self, entity, sequence=None):
		self.create(entity, sequence)

	def copys(self, mlist):
		if self.islogdebug:
			self.logger.debug(mlist)
		for e in mlist:
			self.copy(e)

	def count(self, entityclass, wheres=None, orderby=None):
		return -1


	def query_count(self, entityclass, entity, like=True):
		return -1

	def query(self, entityclass, entity, like=True, lockmode=None):
		return []

	def validate(self, entity, form=None):
		return self.idtvalidate.validate(entity, form)

	def validates(self, entities, form=None):
		return self.idtvalidate.validates(entities, form)

	def ac(self, atype, number, values):
		return self.idtac.can(atype, number, values)

	def export(self, atype, entity, form=None, custom=None, columns=True, todisplay=True):
		return self.idtexport.export(atype, entity, form, custom, columns, todisplay)

	def exports(self, atype, entities, form=None, custom=None, columns=True, todisplay=True):
		return self.idtexport.exports(atype, entities, form, custom, columns, todisplay)

	def imports(self, atype, data, newid=True, update=True, dohash=True):
		return self.idtimport.imports(atype, data, newid)

	def sequence_up(self, entity, values=None):
		return self.idtsequence.sequence_up(entity, values)

	def sequence_remove(self, entity):
		return self.idtsequence.sequence_remove(entity)

	def get_sequence_value_first(self, entity):
		return self.idtsequence.get_sequence_value_first(entity)

	def get_sequence_value_next(self, entity, value):
		return self.idtsequence.get_sequence_value_next(entity, value)

	def get_sequence_master(self, entity):
		return self.idtsequence.get_sequence_master(entity)

	def get_sequence_all(self, entity):
		return self.idtsequence.get_sequence_all(entity)

	def get_sequence_last(self, entity):
		return self.idtsequence.get_sequence_last(entity)

	def get_sequence_next(self, entity):
		return self.idtsequence.get_sequence_next(entity)

	def get_sequence_previous(self, entity):
		return self.idtsequence.get_sequence_previous(entity)

	def sequence_ups(self, mlist):
		if self.islogdebug:
			self.logger.debug(mlist)
		for e in mlist:
			self.sequence_up(e)

	def get_relations(self, entity, relactionclass, fromattribute, wheres=None):
		return self.idtrelation.get_relations(self, entity, relactionclass, fromattribute, wheres)

	def convert_to_utc_datetime_object(self, dt):
		if dt == None:
			return dt

		if self.request == None:
			return dt

		if _utc:
			a = dt
			if dt.tzname() is None:
				a = self.request.timezone.localize(dt)

			return a.astimezone(_utc)
		return dt

	def convert_from_utc_datetime_object(self, dt):
		if dt == None:
			return dt

		if self.request == None:
			return dt

		if _utc:
			a = dt
			if dt.tzname() is None:
				a = _utc.localize(dt)

			return a.astimezone(self.request.timezone)
		return dt

	def get_child_relations(self, entity, relactionclass, wheres=None):
		return self.idtrelation.get_child_relations(self, entity, relactionclass, wheres)

	def get_parent_relations(self, entity, relactionclass, wheres=None):
		return self.idtrelation.get_parent_relations(self, entity, relactionclass, wheres)

	def get_friend_relations(self, entity, relactionclass, wheres=None):
		return self.idtrelation.get_friend_relations(self, entity, relactionclass, wheres)

	def create_child_relation(self, entity, relactionclass, toentity, attributes=None):
		return self.idtrelation.create_child_relation(entity, relactionclass, toentity, attributes)

	def create_parent_relation(self, entity, relactionclass, toentity, attributes=None):
		return self.idtrelation.create_parent_relation(entity, relactionclass, toentity, attributes)

	def create_friend_relation(self, entity, relactionclass, toentity, attributes=None):
		return self.idtrelation.create_friend_relation(entity, relactionclass, toentity, attributes)

	def extends(self, entity, relactionclass, toclass, fromattribute, toattribute, wheres=None):
		return self.idtrelation.extends(self, entity, relactionclass, toclass, fromattribute, toattribute, wheres)

	def extend_childs(self, entity, relactionclass, toclass, wheres=None):
		return self.idtrelation.extend_childs(self, entity, relactionclass, toclass, wheres)

	def extend_parents(self, entity, relactionclass, toclass, wheres=None):
		return self.idtrelation.extend_parents(self, entity, relactionclass, toclass, wheres)

	def extend_friends(self, entity, relactionclass, toclass, wheres=None):
		return self.idtrelation.extend_friends(self, entity, relactionclass, toclass, wheres)

	# ----------------------------------------------------
	## Property
	idtrelation = ESiPCProperty(get_idt_relation, None, None)
	idtsequence = ESiPCProperty(get_idt_sequence, None, None)
	idtcio = ESiPCProperty(get_idt_cio, None, None)
	idtexport = ESiPCProperty(get_idt_export, None, None)
	idtimport = ESiPCProperty(get_idt_import, None, None)
	idtac = ESiPCProperty(get_idt_ac, None, None)
	idtvalidate = ESiPCProperty(get_idt_validate, None, None)
	idtform = ESiPCProperty(get_idt_form, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
